<?php
/**
 * Created by PhpStorm.
 * User: wzy12
 * Date: 2018/9/20
 * Time: 13:49
 */

namespace app\admin\controller;


class Goods extends Base
{
    /**
     * 商品分类
     */
    public function goodsCate(){
        $list = model('GoodsCate') -> resList();
        $this -> assign('list', $list['list']);
        $this -> assign('count', $list['count']);

        return $this -> fetch();
    }

    public function goodsCate_form(){
        if($this -> request -> isPost()){
            $data = input('post.');
            $result = model('GoodsCate') -> operation($data);

            if($result['code']){
                return $this -> success($result['msg'], url('goods/goodsCate'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);
            $type = input("type", false);
            $res = null;
            if($id){
                $res = model('GoodsCate') -> resFind($id);
            }

            $this -> assign('type', $type);
            $this -> assign('res', $res);
            $this -> assign('id', $id);
            return $this -> fetch();
        }
    }

    public function goodsCate_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('GoodsCate') -> del($data);
            if($res['code']){
                return $this -> success($res['msg'], url('goods/goodsCate'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }

    /**
     * 分类排序
     */
    public function cateSort(){
        if($this -> request -> isAjax()){
            $data = input('post.');
            $result = model('GoodsCate') -> editSort($data);

            if($result['code']){
                return $this -> success($result['msg'], url('goods/goodsCate'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求');
        }
    }

    /**
     * 分类状态
     */
    public function cateStatus(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('GoodsCate') -> editStatus($data);

            if($result['code']){
                return $this -> success($result['msg'], url('goods/goodsCate'), ['status' => $data['status']]);
            }else{
                $status = $data['status'] == 1 ? 0 : 1;
                return $this -> error($result['msg'], '', ['status' => $status]);
            }
        }else{
            return $this -> error('非法请求');
        }
    }

    /**
     * 商品列表
     */
    public function goods(){
        $search_key = input();
        unset($search_key['/admin/goods/goods_html']);

        $arr = model('Goods') -> resList($search_key);
        unset($search_key['page']);
        if(!$search_key){
            $search_key = null;
        }

        $this -> assign('search_key', $search_key);
        $this -> assign('list', $arr['list']);
        $this -> assign('goods_cate', $arr['goods_cate']);
        return $this -> fetch();
    }

    public function goods_form(){
        if($this -> request -> isAjax()){
            $data = input('post.');
            $result = model('Goods') -> operation($data);

            if($result['code']){
                return $this -> success($result['msg'], url('goods/goodsCate'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);
            $result = null;
            $group_img = null;
            if($id) {
                $result = model('Goods')->resFind($id);

                // 商品组图
                $group_img = model('GoodsImg') -> where('goods_id', $id) -> select();
            }

            // 商品分类
            $goods_cate = model('Goods') -> resCate();

            // 商品模型
            $goods_model = model('GoodsModel') -> select();

            // 运费模板
            $template = model('FreightTemplate') -> select();

            $this -> assign('result', $result);
            $this -> assign('cate', $goods_cate);
            $this -> assign('group_img', $group_img);
            $this -> assign('goods_model', $goods_model);
            $this -> assign('template', $template);
            $this -> assign('id', $id);
            return $this -> fetch();
        }
    }

    public function goods_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('Goods') -> del($data);
            if($res['code']){
                return $this -> success($res['msg'], url('goods/goods'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }

    /**
     * 商品模型对应的值，属性和规格
     */
    public function spec_attr(){
        if($this -> request -> isAjax()){
            $data = input('model_id');
            $result = model('Goods') -> specAttr($data);

            return $this -> success('', '', $result);
        }else{
            return $this -> error('非法请求');
        }
    }

    /**
     * 商品图片上传
     */
    public function goods_upload(){
        $file = input('file.img');
        $num = 1;

        if(!$file){
            $num = 0;
            $file = input('file.file');
        }

        $msg = $this -> upload($file);
        if(!$msg['code']){
            return $this -> error($msg['msg']);
        }else{
            return echoArr($num, $msg['msg'], ['img' => $msg['data']['img'], 'src' => config('imgRoute') . $msg['data']['img']]);
        }
    }

    /**
     * 商品排序
     */
    public function goodsSort(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('Goods') -> editSort($data);

            if($result['code']){
                return $this -> success($result['msg'], url('goods/goodsCate'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求');
        }
    }

    /**
     * 商品状态
     */
    public function goodsStatus(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('Goods') -> editStatus($data);

            if($result['code']){
                return $this -> success($result['msg'], url('goods/goods'));
            }else{
                return $this -> error($result['msg']);
            }
        }else{
            return $this -> error('非法请求');
        }
    }

    /**
     * 评价管理
     */
    public function comment(){
        $list = model('Comment') -> resList();

        $this -> assign('list', $list);
        return $this -> fetch();
    }

    /**
     * 查看评价
     */
    public function comment_cat(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('Comment') -> send($data);
            if($res['code']){
                $res['data']['data']['add_time'] = date('Y-m-d H:i', $res['data']['data']['add_time']);
                return $this -> success($res['msg'], '', ['data' => $res['data']['data']]);
            }else{
                return $this -> error($res['msg']);
            }
        }else{
            $id = input('id', 0);
            $result = null;
            if($id) {
                $result = model('Comment')->resFind($id);
            }

            $this -> assign('id' ,$id);
            $this -> assign('result', $result);
            return $this -> fetch();
        }
    }

    public function comment_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('Comment') -> del($data);
            if($res['code']){
                return $this -> success($res['msg'], url('goods/comment'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }

    /**
     * 评价是否显示
     */
    public function commentStatus(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('Comment') -> editStatus($data);

            if($result['code']){
                return $this -> success($result['msg'], url('goods/comment'), ['status' => $data['is_show']]);
            }else{
                return $this -> error($result['msg']);
            }
        }else{
            return $this -> error('非法请求');
        }
    }
}